package cn.demomaster.quickui_sample.ui.fragment.helper;

import android.Manifest;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.demomaster.quickcompat.service.ServiceHelper;
import com.demomaster.quickcompat.widget.button.QuickButton;

import cn.demomaster.quicklogger.QdLog;
import cn.demomaster.quickpermission.QuickPermission;
import cn.demomaster.quickpermission.listener.PermissionResultCallback;
import cn.demomaster.quickrouter.fragment.QuickFragment;
import cn.demomaster.quickui.annotation.ActivityPager;
import cn.demomaster.quickui.annotation.ResType;
import cn.demomaster.quickui_sample.R;
import cn.demomaster.quickui_sample.service.TrafficFloatingService;
import cn.demomaster.quickui_sample.ui.activity.utils.WifiUtil;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;


/**
 * 流量监控
 * Squirrel桓
 * 2018/8/25
 */

@ActivityPager(name = "流量", preViewClass = TextView.class, resType = ResType.Custome)
public class TrafficFragment extends QuickFragment {


    QuickButton btn_start;
    QuickButton btn_stop;
    View mView;

    @NonNull
    @Override
    public View onGenerateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(R.layout.fragment_layout_traffic, null);
        }
        return mView;
    }

    public void initView(View rootView) {

        btn_start = findViewById(R.id.btn_start);
        btn_stop = findViewById(R.id.btn_stop);
        btn_start.setOnClickListener(v -> {
                    QuickPermission.with(getActivity())
                            .permissions(new String[]{Manifest.permission.SYSTEM_ALERT_WINDOW,
                                    Manifest.permission.READ_PHONE_STATE, Manifest.permission.PACKAGE_USAGE_STATS})
                            .onGranted(new Function0<Unit>() {
                                @Override
                                public Unit invoke() {
                       /*final AppOpsManager appOps;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                    if (isGrantedUsagePremission(getContext())) {
                        Toast.makeText(getContext(),"ok",Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getContext(),"refused",Toast.LENGTH_SHORT).show();
                        return;
                    }
                }*/
                                    ServiceHelper.stopService(mContext, TrafficFloatingService.class);
                                    return null;
                                }
                            })
                            .request();
                });
        btn_stop.setOnClickListener(v ->{ServiceHelper.stopService(mContext,TrafficFloatingService.class);});
    //QDTcpClient.setStateListener();
}
}